package collection;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapList extends JInternalFrame {
	private JTextField findStr;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;
	private JScrollPane scrollPane;
	private JTextArea result;

	Map<String, MapData> map = new HashMap<String, MapData>();

	// /**
	// * Launch the application.
	// */
	// public static void main(String[] args) {
	// EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// try {
	// MapList frame = new MapList();
	// frame.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// });
	// }

	/**
	 * Create the frame.
	 */
	public MapList(HashMap<String, MapData> a) {
		super("List", true, true, true, false);
		this.map = a;

		setBounds(100, 100, 283, 284);
		getContentPane().setLayout(null);
		getContentPane().add(getFindStr());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getBtnNewButton_2());
		getContentPane().add(getScrollPane());

	}

	private JTextField getFindStr() {
		if (findStr == null) {
			findStr = new JTextField();
			findStr.setBounds(12, 20, 133, 21);
			findStr.setColumns(10);
		}
		return findStr;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String find = findStr.getText();
					boolean flag = false;
					result.setText("");
					int cnt = 0;

					if (find.equals("")) {
						result.append("검색어를 입력하세요.\n");
					} else {
						Collection<MapData> col = map.values();
						Iterator<MapData> it = col.iterator();

						while (it.hasNext()) {
							MapData d = it.next();

							if (d.getIrum().contains(find)
									|| d.getPhone().contains(find)
									|| d.getUserID().contains(find)) {
								flag = true;
								result.append(d.toString());
								cnt++;
							}
						}
					}
				}
			});
			btnNewButton.setBounds(157, 19, 97, 23);
		}
		return btnNewButton;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("모든 키");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Set<String> set = map.keySet();
					Iterator<String> it = set.iterator();
					int cnt = 0;

					result.setText("KEYS_______________\n");
					while (it.hasNext()) {
						result.append(it.next());
						result.append("\n");
						cnt++;
					}
					result.append("총건수:" + cnt + "\n");
					findStr.setText("출력이 완료되었습니다.");
				}
			});
			btnNewButton_1.setBounds(12, 61, 97, 23);
		}
		return btnNewButton_1;
	}

	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("모든 데이터");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String str = String.format("%-16s%-16s%-16s%-18s\n", "아이디",
							"성명", "연락처", "일자");
					int cnt = 0;
					result.setText(str);
					Collection<MapData> col = map.values();
					Iterator<MapData> it = col.iterator();

					while (it.hasNext()) {
						MapData d = it.next();
						result.append(d.toString());
						cnt++;
					}
					findStr.setText(cnt + "건의 출력이 완료 되었습니다.");
				}
			});
			btnNewButton_2.setBounds(136, 61, 118, 23);
		}
		return btnNewButton_2;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 100, 242, 143);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}

	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
}
